package ca.llsutherland.squash.domain.helper;

import ca.llsutherland.squash.domain.Administrator;
import ca.llsutherland.squash.domain.Clock;
import ca.llsutherland.squash.domain.Gym;
import ca.llsutherland.squash.exceptions.ValidationException;
import ca.llsutherland.squash.utils.ErrorConstants;
import ca.llsutherland.squash.utils.StringUtils;

public class GymsHelper {
    
    public static Gym createUnpersistedGym(String name, Clock dateCreated, Clock dateUpdated) {
        return new Gym(name, dateCreated, dateUpdated);
    }

    public static Gym createUnpersistedGym(String name, Clock dateCreated, Clock dateUpdated, String phoneNumber) {
        Gym gym = createUnpersistedGym(name, dateCreated, dateUpdated);
        gym.setPhoneNumber(phoneNumber);
        return gym;
    }

    public static Gym createUnpersistedGym(String name, Clock dateCreated, Clock dateUpdated, String phoneNumber, String slogan) {
        Gym gym = createUnpersistedGym(name, dateCreated, dateUpdated, phoneNumber);
        gym.setSlogan(slogan);
        return gym;
    }
    
    public static Gym createUnpersistedGym(String name, Clock dateCreated, Clock dateUpdated, String phoneNumber, String slogan, String streetAddress) {
        Gym gym = createUnpersistedGym(name, dateCreated, dateUpdated, phoneNumber, slogan);
        gym.setStreetAddress(streetAddress);
        return gym;
    }
    
    public static Gym createNewGym(String name, Clock dateCreated, Clock dateUpdated, String phoneNumber, String slogan, String streetAddress, Long id, Administrator lastUpdatedBy) {
        Gym gym = createUnpersistedGym(name, dateCreated, dateUpdated, phoneNumber, slogan, streetAddress, lastUpdatedBy);
        gym.setId(id);
        return gym;
    }
    
    public static Gym createUnpersistedGym(String name, Clock dateCreated, Clock dateUpdated, String phoneNumber, String slogan, String streetAddress, Administrator lastUpdatedBy) {
        Gym gym = createUnpersistedGym(name, dateCreated, dateUpdated, phoneNumber, slogan, streetAddress);
        gym.setLastUpdatedBy(lastUpdatedBy);
        return gym;
    }
    
    public static void assertValid(Gym gym) throws ValidationException {
        DomainObjectHelper.assertValidId(gym.getId());
        assertNewValid(gym);
    }
    
    public static void assertNewValid(Gym gym) {
        assertValidGymName(gym.getName());
        assertValidPhoneNumber(gym.getPhoneNumber());
        if(gym.getLastUpdatedBy() == null) {
            throw new ValidationException(ErrorConstants.NULL_ADMINISTRATOR_ERROR);
        }
        AdministratorsHelper.assertValidAdministrator(gym.getLastUpdatedBy());
    }

    private static void assertValidPhoneNumber(String phoneNumber) {
        if(!StringUtils.isBlankString(phoneNumber)) {
            if(phoneNumber.length() < 7) {
                throw new ValidationException(ErrorConstants.INVALID_PHONE_NUMBER_LENGTH_ERROR);
            }
        }
    }

    private static void assertValidGymName(String name) {
        DomainObjectHelper.assertNonBlankStringWithErrorMessage(name, ErrorConstants.INVALID_GYM_NAME_ERROR);
        try {
            NamedDomainObjectHelper.assertValidName(name);
        }catch(ValidationException vex) {
            throw new ValidationException(ErrorConstants.INVALID_GYM_NAME_ERROR);
        }
    }

}
